package com.example.weibo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.bumptech.glide.Glide;
import com.example.weibo.MessageItem;
import com.example.weibo.R;

import java.util.List;

public class MessageListAdapter extends ArrayAdapter<MessageItem> {
    private int mResource;
    public MessageListAdapter(@NonNull Context context, int resource, @NonNull List<MessageItem> objects) {
        super(context, resource, objects);
        mResource  = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(mResource, parent, false);
        MessageItem messageItem = getItem(position);
        // 渲染图片
        ImageView imageView = view.findViewById(R.id.image);
        Glide.with(getContext()).load(messageItem.getImage()).into(imageView);
        // 渲染标题
        TextView titleView = view.findViewById(R.id.title);
        titleView.setText(messageItem.getTitle());
        // 渲染内容
        TextView contentView = view.findViewById(R.id.content);
        contentView.setText(messageItem.getContent());
        // 渲染内容
        TextView timeView = view.findViewById(R.id.time);
        timeView.setText(messageItem.getTime());
        return view;
    }
}

